package com.gaoyi.crm.fragment.mtc;

import androidx.activity.result.ActivityResult;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.gaoyi.crm.R;
import com.gaoyi.crm.adapter.SyncedPmnAdapter;
import com.gaoyi.crm.callback.IView;
import com.gaoyi.crm.constant.ApiStatus;
import com.gaoyi.crm.control.dialog.ConfirmDialog;
import com.gaoyi.crm.control.dialog.LoadingDialogUtils;
import com.gaoyi.crm.databinding.FragmentMtcSyncedListBinding;
import com.gaoyi.crm.db.dto.PmnDto;
import com.gaoyi.crm.db.query.MtcSyncedQuery;
import com.gaoyi.crm.fragment.base.BaseChildListFragment;
import com.gaoyi.crm.util.ToastUtil;
import com.gaoyi.crm.util.Utils;
import com.gaoyi.crm.vm.MtcSyncedListViewModel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class MtcSyncedListFragment extends BaseChildListFragment<FragmentMtcSyncedListBinding, MtcSyncedListViewModel, PmnDto, MtcSyncedQuery> implements IView {

    private SyncedPmnAdapter mAdapter;
    private ConfirmDialog mDialog;
    private List<String> mIds;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mtc_synced_list;
    }

    @Override
    protected void initView() {
        mDateSearchView = mBinding.mobileInclude.viewDateSearch;
        mViewSearch = mBinding.mobileInclude.viewSearch;
        mSmartRefreshLayout = mBinding.smartRefreshLayout;
        serTitleSort(mBinding.titleSyncDate);
        mAdapter = new SyncedPmnAdapter(mContext);
        mAdapter.setOnItemClickListener((v, position) -> mAdapter.changeSelected(position));
        mBinding.rvSynced.setLayoutManager(new LinearLayoutManager(mContext));
        mBinding.rvSynced.setItemAnimator(null);
        mBinding.rvSynced.setAdapter(mAdapter);
        mBinding.btnDelete.setOnClickListener(view ->  delete());
    }

    private void delete() {
        List<PmnDto> list = mAdapter.getCurrentList();
        if (list.isEmpty()) {
            ToastUtil.showToast(R.string.no_data);
            return;
        }
        if (mIds == null) {
            mIds = new ArrayList<>();
        }
        mIds.clear();
        for (PmnDto dto : list) {
            if (dto.isCheck()) {
                mIds.add(dto.getId());
            }
        }
        if (mIds.isEmpty()) {
            ToastUtil.showToast(R.string.p_sel);
            return;
        }
        if (mDialog == null) {
            mDialog = ConfirmDialog.newInstance(mContext, R.string.delete_record, () -> mViewModel.delete(mIds));
            mDialog.show(getChildFragmentManager());
        } else {
            mDialog.show();
        }
    }

    @Override
    protected void initData() {
        if (isPause) { // 跳转后返回，SearchView会自动调用onQueryTextChange请求数据
            return;
        }
        showLoading();
        mQuery.init();
        mQuery.setSyncedStatus(ApiStatus.SYNC_SUCCESS);
        mViewModel.getSyncedPmn(mQuery);
    }

    @Override
    protected void initLiveData() {
        mViewModel.getTotal().observe(mParent, integer -> mBinding.keyValueSynced.setValue(new String[]{getString(R.string.total_item, integer)}));
        mViewModel.getResultCode().observe(mParent, integer -> Utils.refreshSmartRefreshLayout(mBinding.smartRefreshLayout));
        mViewModel.getList().observe(mParent, pmnSyncDtos -> mAdapter.submitList(pmnSyncDtos));
        mViewModel.getDeleteCode().observe(mParent,integer -> search(mViewSearch.getQuery().toString()));
    }

    @Override
    protected void loadData() {
        mViewModel.getSyncedPmn(mQuery);
    }

    @Override
    protected void search(Date startDate, Date endDate) {
        mViewModel.clearListValue();
        mQuery.setEndDate(endDate.getTime());
        mQuery.setStartDate(startDate.getTime());
        mQuery.setKeyWord(mViewSearch.getQuery().toString());
        mQuery.init();
        mViewModel.getSyncedPmn(mQuery);
    }

    @Override
    protected void search(String keyWord) {
        mViewModel.clearListValue();
        if (mDateSearchView.isSelect()) {
            mQuery.setStartDate(mDateSearchView.getStartDate().getTime());
            mQuery.setEndDate(mDateSearchView.getEndDate().getTime());
        }
        mQuery.setKeyWord(keyWord);
        mQuery.init();
        mViewModel.getSyncedPmn(mQuery);
    }

    @Override
    protected void sort(int id, Integer sort) {
        mQuery.setOrderBySyncedAt(sort);
        mViewModel.clearListValue();
        mQuery.init();
        mViewModel.getSyncedPmn(mQuery);
    }

    @Override
    public void onPause() {
        super.onPause();
        isPause = true;
    }

    @Override
    protected MtcSyncedQuery createQuery() {
        return new MtcSyncedQuery();
    }

    @Override
    protected void onRequestPermissionsResult(Map<String, Boolean> result) {

    }

    @Override
    protected void onActivityResult(int requestCode, ActivityResult result) {

    }

    @Override
    public void showLoading() {
        LoadingDialogUtils.getInstance().showLoading(mContext);
    }

    @Override
    public void hideLoading() {
        LoadingDialogUtils.getInstance().dismiss();
    }

}